Read all persons
using (EntitiesModel dbContext = new EntitiesModel())
{
foreach (var person in dbContext.Persons)
{
string firstName = person.FirstName;
}
}
More info and samples on: www.devarchweb.net
Add new personusing (EntitiesModel dbContext = new EntitiesModel())
{
Persons newPerson = new Persons();
newPerson.FirstName = "New";
dbContext.Add(newPerson);
dbContext.SaveChanges();
}
More info and samples on: www.devarchweb.net
Update Person with Id 1using (EntitiesModel dbContext = new EntitiesModel())
{
Persons person = dbContext.Persons.Where(p => p.Id==1).FirstOrDefault();
person.LastName += "++";
dbContext.SaveChanges();
}
More info and samples on: www.devarchweb.net
Delete persons with Id > 1using (EntitiesModel dbContext = new EntitiesModel())
{
IQueryable<Persons> personsToDelete = dbContext.Persons.Where(p => p.Id > 1);
dbContext.Delete(personsToDelete);
// dbContext.Delete(personsToDelete.FirstOrDefault()); One record can be deleted as well
dbContext.SaveChanges(Telerik.OpenAccess.ConcurrencyConflictsProcessingMode.AggregateAll);
}
More info and samples on: www.devarchweb.net
Concurency handling during save// executes all SQL statements regardless of error
dbContext.SaveChanges(Telerik.OpenAccess.ConcurrencyConflictsProcessingMode.AggregateAll);
// stops the processing after the first error occurs
dbContext.SaveChanges(Telerik.OpenAccess.ConcurrencyConflictsProcessingMode.StopOnFirst);
More info and samples on: www.devarchweb.net
Automatic join with WHERE (persons and departments) Normally it possibl eto write join between 2 tables like belowvar people2 = dbContext.Persons
.Join(
dbContext.Departments,
p => p.Department_id,
d => d.Id,
(p, d) => new { name = p.FirstName, name1 = p.LastName, d.Name }
);
var people3 = from p in dbContext.Persons
from d in dbContext.Departments
where p.Department_id == d.Id // works like JOIN
select new { p.FirstName, p.LastName, d.Name };
SELECT a.[firstName] AS COL1, a.[lastName] AS COL2, b.[name] AS COL3
FROM [MY_SCHEMA].[persons] a
CROSS JOIN [MY_SCHEMA].[departments] AS b
WHERE a.[department_id] = b.[id]
More info and samples on: www.devarchweb.net
Use navigation property to read Department name for every person It is possible to use Navigation to access data in related tablesforeach (Persons person in dbContext.Persons)
{
string departmenName = person.Departments.Name;
}
SELECT a.[id] AS COL1, a.[birthday] AS COL2, a.[department_id] AS COL3, a.[department_id] AS COL4, a.[firstName] AS COL5, a.[height] AS COL6, a.[lastName] AS COL7
FROM [MY_SCHEMA].[persons] a
ORDER BY COL1
declare @p1 int
set @p1=2
exec sp_prepexec @p1 output,N'@p0 int',N'SELECT [head] AS COL1, [name] AS COL2 FROM [MY_SCHEMA].[departments] WHERE [id] = @p0',@p0=1
select @p1
declare @p1 int
set @p1=2
exec sp_prepexec @p1 output,N'@p0 int',N'SELECT [head] AS COL1, [name] AS COL2 FROM [MY_SCHEMA].[departments] WHERE [id] = @p0',@p0=2
select @p1
More info and samples on: www.devarchweb.net
Join with in memory list and data in DB (person and departments ids) Telerik enables writing queries that join data in memeory with data in SQL databasevar persons5 = (from p in dbContext.Persons
where p.FirstName == ("John")
join d in deptIds on p.Department_id equals d
select p)
.ToList();
SELECT a.[id] AS COL1, a.[birthday] AS COL2, a.[department_id] AS COL3, a.[department_id] AS COL4, a.[firstName] AS COL5, a.[height] AS COL6, a.[lastName] AS COL7
FROM [MY_SCHEMA].[persons] a
WHERE a.[firstName] = 'John'
ORDER BY COL1
More info and samples on: www.devarchweb.net
How will SQL query look like when WHERE in before or after JOIN) The example above shows that WHERE clause was present. It is the case only when WHERE clauseList<int> deptIds = new List<int> { 1, 2 }; // user selects ids in UI
var persons6 = (from p in dbContext.Persons
join d in deptIds on p.Department_id equals d
where p.FirstName == ("John")
select p)
.ToList();
SELECT a.[id] AS COL1, a.[birthday] AS COL2, a.[department_id] AS COL3, a.[department_id] AS COL4, a.[firstName] AS COL5, a.[height] AS COL6, a.[lastName] AS COL7
FROM [MY_SCHEMA].[persons] a
ORDER BY COL1
More info and samples on: www.devarchweb.net
When will ORDER BY (unexpectedly) present in SQL queryvar persons12 = (from p in dbContext.Persons
select p
).ToList();
SELECT a.[id] AS COL1, a.[birthday] AS COL2, a.[department_id] AS COL3, a.[department_id] AS COL4, a.[firstName] AS COL5, a.[height] AS COL6, a.[lastName] AS COL7
FROM [MY_SCHEMA].[persons] a
ORDER BY COL1
var persons13 = (from p in dbContext.Persons
select new { p.LastName }
).ToList();
SELECT a.[lastName] AS COL1
FROM [MY_SCHEMA].[persons] a
var persons14 = (from p in dbContext.Persons
select new { p.FirstName, p.LastName }
).ToList();
SELECT a.[firstName] AS COL1, a.[lastName] AS COL2
FROM [MY_SCHEMA].[persons] a
var persons15 = (from p in dbContext.Persons
orderby p.LastName
select p
).ToList();
SELECT a.[id] AS COL1, a.[birthday] AS COL2, a.[department_id] AS COL3, a.[department_id] AS COL4, a.[firstName] AS COL5, a.[height] AS COL6, a.[lastName] AS COL7
FROM [MY_SCHEMA].[persons] a
ORDER BY COL7, COL1
More info and samples on: www.devarchweb.net
How will be join tables for N to relationship in model handled If you have two tables with n to n relationship likeCREATE TABLE [MY_SCHEMA].[persons](
[id] [int] IDENTITY(1,1) NOT NULL,
[firstName] [varchar](20) NULL,
[lastName] [varchar](20) NOT NULL,
)
CREATE TABLE [MY_SCHEMA].[roles](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL, -- e.g. Manager, Engineer, Accountant
[Description] [nvarchar](max) NULL,
)
CREATE TABLE [MY_SCHEMA].[persons_roles](
[Person_ID] [int] NOT NULL,
[Role_ID] [int] NOT NULL
)
var personsInRole = dbContext.Persons
.Where(p => p.Roles.Where(role => role.Name == "Manager").Count() > 0 );